package com.kundi.dooradmin.controller;

import com.kundi.dooradmin.entity.CompanyDynamic;
import com.kundi.dooradmin.service.CompanyDynamicService;
import com.kundi.dooradmin.util.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author 赵恒
 * Date 2020/6/17
 */
@Controller
@RequestMapping("/companyDynamic")
public class CompanyDynamicController {
    @Resource
    CompanyDynamicService companyDynamicService;

    /**
     * 添加公司动态
     * @param companyDynamic
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> addDynamic(CompanyDynamic companyDynamic) {
        Map<String, String> map = new HashMap<>();
        Integer i = companyDynamicService.add(companyDynamic);
        if(i > 0) {
            map.put("message", "添加成功");
            map.put("success", "true");
        } else {
            map.put("message", "添加失败");
            map.put("message", "false");
        }

        System.out.println(map);
        return map;
    }

    /**
     * 获取所有的公司动态
     * @return
     */
    @RequestMapping("getAll")
    @ResponseBody
    public List<CompanyDynamic> getAll() {
        return companyDynamicService.getAll();
    }

    /**
     * 获取最近的动态信息
     * @return
     */
    @RequestMapping("/getRecentDynamic")
    @ResponseBody
    public List<CompanyDynamic> getRecentDynamic() {
        return companyDynamicService.getRecentDynamic();
    }

    /**
     * 通过ID获取内容content
     * @param id
     * @return
     */
    @RequestMapping(value = "getContentById", method = RequestMethod.GET)
    @ResponseBody
    public String getContentById(Integer id) {
        String content = companyDynamicService.getContentById(id);
        return content;
    }

    /**
     * 通过ID获取对应的动态对象
     * @param id
     * @return
     */
    @RequestMapping("/getDynamicById")
    @ResponseBody
    public CompanyDynamic getDynamicById(Integer id) {
        return companyDynamicService.getDynamicById(id);
    }

    /**
     * 修改公司动态
     * @param companyDynamic
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public Map<String, String> update(CompanyDynamic companyDynamic) {
        Integer i = companyDynamicService.update(companyDynamic);
        if(i > 0) {
            return MapUtil.responseMap("修改成功", "true");
        } else {
            return MapUtil.responseMap("修改失败", "false");
        }
    }

    @RequestMapping("/deleteDynamicById")
    @ResponseBody
    public Map<String, String> delete(Integer id) {

        Integer i = companyDynamicService.delete(id);
        if(i > 0) {
            return MapUtil.responseMap("删除成功", "true");
        } else {
            return MapUtil.responseMap("删除失败", "false");
        }
    }
}
